/*
 *  Copyright 2019-2020 Sun Feng
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.ruibo.modules.weixin.rest;

import com.alibaba.excel.EasyExcel;
import com.ruibo.annotation.AnonymousAccess;
import com.ruibo.aop.log.Log;
import com.ruibo.config.FileProperties;
import com.ruibo.modules.security.security.vo.JwtUser;
import com.ruibo.modules.weixin.domain.DustReport;
import com.ruibo.modules.weixin.domain.excel.DustReportExcel;
import com.ruibo.modules.weixin.service.DustReportService;
import com.ruibo.modules.weixin.service.dto.DustReportDto;
import com.ruibo.modules.weixin.service.dto.DustReportQueryCriteria;
import com.ruibo.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 孙峰
 * @website https://el-admin.vip
 * @date 2020-11-06
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/weixin/dustReport")
public class DustReportController {

    private final DustReportService dustReportService;

    private final FileProperties fileProperties;

    @Log("导出数据")
    @GetMapping(value = "/export")
    public ResponseEntity<Object> export(final HttpServletRequest request, final HttpServletResponse response, DustReportQueryCriteria criteria) throws IOException {
        List<DustReportExcel> dustReportExcels = dustReportService.queryAllForeExcel(criteria);

        String fileName = UUID.randomUUID() + ".xlsx";
        String abstractFile = fileProperties.getPath().getPath() + fileName;
        File file = new File(fileProperties.getPath().getPath());

        if(!file.exists()){
            file.mkdirs();
        }

        EasyExcel.write(abstractFile, DustReportExcel.class).sheet("模板").doWrite(dustReportExcels);
        return new ResponseEntity<>(fileName, HttpStatus.OK);
    }


    @GetMapping
    @Log("查询DustReport")
    public ResponseEntity<Object> query(DustReportQueryCriteria criteria, Pageable pageable) {

        return new ResponseEntity<>(dustReportService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    /**
     * 更具id获取上报信息
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<Object> findById(@PathVariable("id") long id) {
        DustReportDto dustReportDto = dustReportService.findById(id);
        return new ResponseEntity<>(dustReportDto, HttpStatus.OK);
    }

    /**
     * 更具微信用户的openid 获取数据列表信息
     *
     * @param openId
     * @return
     */
    @AnonymousAccess
    @GetMapping("/openId/{openId}")
    public ResponseEntity<Object> findByOpenId(@PathVariable("openId") String openId) {
        List<DustReport> dustReportList = dustReportService.findByOpenid(openId);
        return new ResponseEntity<>(dustReportList, HttpStatus.OK);
    }


    @PostMapping
    @Log("新增DustReport")
    @AnonymousAccess
    public ResponseEntity<Object> create(@Validated @RequestBody DustReportDto resources) {
        return new ResponseEntity<>(dustReportService.create(resources), HttpStatus.CREATED);
    }




    @PutMapping
    @Log("修改DustReport")
    public ResponseEntity<Object> update(@Validated @RequestBody DustReportDto resources) {
        dustReportService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除DustReport")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        dustReportService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }


}
